<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use ReflectionClass;

class Export extends Controller
{
    //通用导出数据方法
    public function d(Request $request)
    {
        //获取模块控制器方法
        $m = input("param.eee_m");//获取数据的模块
        $c = input("param.eee_c");//获取数据的控制器
        $f = input("param.eee_f");//获取数据的方法
        $c = ucfirst($c);//大写控制器第一个字母
        $title = input("param.eee_t");//获取页面标题
        $colstr = input("param.eee_l");
        $col = json_decode($colstr);//获取表格列

        //表格头行数
        $header_rows = count($col);

        //表格列数
        $header_cols = 0;
        for ($i = 0; $i < count($col[0]); $i++) {
            if (isset($col[0][$i]->colspan)) {
                $header_cols += $col[0][$i]->colspan;
            } else {
                $header_cols += 1;
            }
        }

        //excel列名称
        $columnName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');

        //反射获取数据
        $class = "app\\" . $m . "\\controller\\" . $c;
        $class = new ReflectionClass($class);
        $instance = $class->newInstanceArgs();
        $method = $class->getmethod($f);
        $obj = $method->invoke($instance);
        $data = $obj->getData();

        if (array_key_exists("rows", $data)) {
            $data = $data["rows"];
        }
        $rows = count($data);

        //创建excel对象
        $objPHPExcel = new \PHPExcel();

        //设置excel属性
        $Creator = iconv('utf-8', 'gb2312', "苏州普腾信息科技有限公司");
        $LastModifiedBy = iconv('utf-8', 'gb2312', "普腾软件");
        $Title = iconv('utf-8', 'gb2312', "Office 2007 XLSX Export Document");
        $Subject = iconv('utf-8', 'gb2312', "Office 2007 XLSX Export Document");
        $Description = iconv('utf-8', 'gb2312', "the document export from " . config("system_name"));

        $objPHPExcel->getProperties()->setCreator($Creator);
        $objPHPExcel->getProperties()->setLastModifiedBy($LastModifiedBy);
        $objPHPExcel->getProperties()->setTitle($Title);
        $objPHPExcel->getProperties()->setSubject($Subject);
        $objPHPExcel->getProperties()->setDescription($Description);

        //设置活动sheet
        $objPHPExcel->setActiveSheetIndex(0);

        //获取活动sheet
        $sheet = $objPHPExcel->getActiveSheet();

        //设置sheet标题
        $sheet->setTitle($title);

        //设置列宽
        for ($i = 0; $i < $header_cols; $i++) {
            $sheet->getColumnDimension($columnName[$i])->setWidth(15);
        }

        //设置边框颜色
        $sheet->getStyle("A1:" . $columnName[$header_cols - 1] . "" . ($rows + $header_rows))->getBorders()->getAllBorders()->getColor()->setRGB('666666');
        $sheet->getStyle("A1:" . $columnName[$header_cols - 1] . "" . ($rows + $header_rows))->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);

        //设置表头行高
        for ($i = 0; $i < $header_rows; $i++) {
            $sheet->getRowDimension($i + 1)->setRowHeight(25);
        }

        //设置表头加粗居中背景色
        $sheet->getStyle("A1:" . $columnName[$header_cols - 1] . $header_rows)->getFont()->setBold(true);
        $sheet->getStyle("A1:" . $columnName[$header_cols - 1] . $header_rows)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $sheet->getStyle("A1:" . $columnName[$header_cols - 1] . $header_rows)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $sheet->getStyle("A1:" . $columnName[$header_cols - 1] . $header_rows)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $sheet->getStyle("A1:" . $columnName[$header_cols - 1] . $header_rows)->getFill()->getStartColor()->setRGB('c5d9f1');

        //设置数据字体大小
        $sheet->getStyle("A" . ($header_rows + 1) . ":" . $columnName[$header_cols - 1] . "" . ($rows + $header_rows + 1))->getFont()->setSize(10);


        //设置数据行高
        for ($i = 0; $i < count($data); $i++) {
            $sheet->getRowDimension($i + $header_rows + 1)->setRowHeight(17);
        }

        //设置表头数据
        for ($i = 0; $i < count($col); $i++) {
            $col_index = 0;
            for ($j = 0; $j < count($col[$i]); $j++) {
                $cell = $columnName[$col_index];
                $cell = $cell . ($i + 1);
                $val = $col[$i][$j]->title;
                $sheet->SetCellValue($cell, $val);

                if (isset($col[$i][$j]->colspan)) {
                    $col_index += $col[$i][$j]->colspan;
                    $cell1 = $columnName[$col_index - 1];
                    $cell1 = $cell1 . ($i + 1);
                    $sheet->mergeCells($cell . ":" . $cell1);
                } else {
                    $col_index += 1;
                }
            }
        }

        //拼装数据
        for ($i = 0; $i < count($data); $i++) {
            for ($j = 0; $j < count($col[$header_rows - 1]); $j++) {
                $cell = $columnName[$j] . ($i + $header_rows + 1);
                $field = $col[$header_rows - 1][$j]->field;
                if (array_key_exists($field, $data[$i])) {
                    $val = $data[$i][$field];
                    if ($field == "create_time") {
                        if ($val == null || $val == 0) {
                            $val = "";
                        } else {
                            $val = date("Y-m-d H:i:s", $val);
                        }
                    }
                } else {
                    $val = "";
                }

                $sheet->SetCellValue($cell, $val);
            }
        }

        //设置边框颜色 放到最后焦点可以在右下角
        $sheet->getStyle("A1:" . $columnName[$header_cols - 1] . "" . ($rows + $header_rows))->getBorders()->getAllBorders()->getColor()->setRGB('666666');
        $sheet->getStyle("A1:" . $columnName[$header_cols - 1] . "" . ($rows + $header_rows))->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);

        //输入导出信息
        $sheet->SetCellValue("A" . ($rows + $header_rows + 2), "Export");
        $sheet->SetCellValue("B" . ($rows + $header_rows + 2), date('Y-m-d H:i:s'));

        $sheet->SetCellValue("D" . ($rows + $header_rows + 2), "By");
        $sheet->SetCellValue("E" . ($rows + $header_rows + 2), get_user()['name']);

        //输出excel文件
        ob_end_clean();
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $title . '.xls"');
        header("Content-Disposition:attachment;filename=" . $title . ".xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }

}



